package com.wxblockchain.core.network.resParser;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.wxblockchain.core.network.consts.NetworkConstants;
import com.wxblockchain.core.utils.LogUtil;

/**
 * Created by louyaming on 2020/04/21.
 * 业务数据解析时使用的结果封装,这里会对请求结果做统一过滤
 * 非网络错误会在这里标记出来
 */
public class BusinessResult {
    public boolean isSuccess = true;
    public String errorString = "";

    public BusinessResult() {}

    public BusinessResult(boolean success) {
        isSuccess = success;
    }

    public BusinessResult(String s) {
        if (!TextUtils.isEmpty(s)) {
            try {
                BaseResponse response = new Gson().fromJson(s, BaseResponse.class);
                if (response == null) {
                    isSuccess = false;
                    return;
                }
                if (response.code == NetworkConstants.CODE_BUSINESS_SUCCESS) {
                    errorString = "";
                } else {
                    LogUtil.d("数据异常!response code:" + response.code);
                    isSuccess = false;
                    errorString = response.message;
                }
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
    }

    public BusinessResult(BaseResponse response) {
        if (response == null) {
            isSuccess = false;
            return;
        }
        if (response.code == NetworkConstants.CODE_BUSINESS_SUCCESS) {
            errorString = "";
        } else {
            LogUtil.d("数据异常!response code:" + response.code);
            isSuccess = false;
            errorString = response.message;
        }
    }
}
